import axios, { AxiosError } from 'axios';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { configs } from '../../src/config';
import { UserInfo, UserSessionInfo } from '../model/user';

export interface AsyncError extends AxiosError { }

// const agent = process.env.APP_ENV === 'localhost' && {
//   httpsAgent: new HttpsProxyAgent('http://168.219.61.252:8080'),
// };

const agent = process.env.APP_ENV === 'localhost111' && {
  httpsAgent: new HttpsProxyAgent('http://168.219.61.252:8080'),
};

export function createHttpClient() {
  return axios.create({
    ...agent, // in Suwon proxy
    withCredentials: true,
  });
}

export const createSessionUser = async (user: UserInfo): Promise<UserSessionInfo> => {
  const response = await createHttpClient().post(`${configs.apiServer}/v1/user`, user);
  return response.data;
};

export const fetchUserByLoginId = async (loginId: string): Promise<UserSessionInfo> => {
  const response = await createHttpClient().get(`${configs.apiServer}/v1/user`, {
    params: { loginId: `${loginId}` },
  });
  return response.data;
};

export const updateUserById = async (user: UserSessionInfo): Promise<UserSessionInfo> => {
  const response = await createHttpClient().put(
    `${configs.apiServer}/v1/users/${user.uid}`,
    user,
  );
  return response.data;
};

export const fetchUserDepartmentById = async (depId: string): Promise<any> => {
  const response = await createHttpClient().get(
    `${configs.bdpApiBaseUrl}/user/getUpperDepartmemtInfo/${depId}/`,
    {
      headers: {
        token: configs.bdpApiToken,
      },
    },
  );
  return response.data;
};
